package org.eco.system.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.eco.common.core.core.domain.model.LoginUser;
import org.eco.common.core.core.page.PageResult;
import org.eco.common.core.utils.MapstructUtils;
import org.eco.common.core.utils.bean.BeanUtils;
import org.eco.common.excel.entity.ExcelResultRes;
import org.eco.common.orm.core.page.PageQuery;
import org.eco.common.orm.core.service.impl.BaseServiceImpl;
import org.eco.system.domain.ImportExport;
import org.eco.system.domain.bo.ImportExportBo;
import org.eco.system.domain.vo.ImportExportVo;
import org.eco.system.mapper.ImportExportMapper;
import org.eco.system.service.IImportExportService;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.eco.system.domain.table.ImportExportTableDef.IMPORT_EXPORT;

/**
 * 导入导出日志Service业务层处理
 *
 * @author wgk
 * @date 2024-06-24
 */
@Service
public class ImportExportServiceImpl extends BaseServiceImpl<ImportExportMapper, ImportExport> implements IImportExportService {

    @Override
    public QueryWrapper query() {
        return super.query().from(IMPORT_EXPORT);
    }

    private QueryWrapper buildQueryWrapper(ImportExportBo importExportBo) {
        QueryWrapper queryWrapper = super.buildBaseQueryWrapper();
        queryWrapper.and(IMPORT_EXPORT.NAME.like(importExportBo.getName()));
        queryWrapper.and(IMPORT_EXPORT.LOG_INFO.eq(importExportBo.getLogInfo()));
        queryWrapper.and(IMPORT_EXPORT.STATUS.eq(importExportBo.getStatus()));
        queryWrapper.and(IMPORT_EXPORT.TYPE.eq(importExportBo.getType()));
        queryWrapper.orderBy(IMPORT_EXPORT.UPDATE_TIME.desc());
        return queryWrapper;
    }

    /**
     * 查询导入导出日志
     *
     * @param id 导入导出日志主键
     * @return 导入导出日志
     */
    @Override
    public ImportExportVo selectById(Long id) {
        return this.getOneAs(query().where(IMPORT_EXPORT.ID.eq(id)), ImportExportVo.class);

    }

    /**
     * 查询导入导出日志列表
     *
     * @param importExportBo 导入导出日志Bo
     * @return 导入导出日志集合
     */
    @Override
    public List<ImportExportVo> selectList(ImportExportBo importExportBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(importExportBo);
        return this.listAs(queryWrapper, ImportExportVo.class);
    }

    /**
     * 分页查询导入导出日志列表
     *
     * @param importExportBo 导入导出日志Bo
     * @return 分页导入导出日志集合
     */
    @Override
    public PageResult<ImportExportVo> selectPage(ImportExportBo importExportBo) {
        QueryWrapper queryWrapper = buildQueryWrapper(importExportBo);
        Page<ImportExportVo> page = this.pageAs(PageQuery.build(), queryWrapper, ImportExportVo.class);
        return PageResult.build(page);
    }

    /**
     * 新增导入导出日志
     *
     * @param importExportBo 导入导出日志Bo
     * @return 结果:true 操作成功，false 操作失败
     */
    @Override
    public boolean insert(ImportExportBo importExportBo) {
        ImportExport importExport = MapstructUtils.convert(importExportBo, ImportExport.class);
        return this.save(importExport);//使用全局配置的雪花算法主键生成器生成ID值
    }

    @Override
    public boolean saveInfo(ExcelResultRes result, LoginUser loginUser, String type) {
        ImportExportBo bo = new ImportExportBo();
        BeanUtils.copyProperties(result, bo);
        bo.setUpdateBy(loginUser.getUserId());
        bo.setCreateBy(loginUser.getUserId());
        bo.setType(type);
        ImportExport importExport = MapstructUtils.convert(bo, ImportExport.class);
        return this.save(importExport);//使用全局配置的雪花算法主键生成器生成ID值
    }

}
